import urllib

import pandas as pd
from matplotlib import pyplot as plt
import mpl_finance as mpf
from matplotlib.pylab import date2num
from pylab import *
import os



import requests,pymysql
from urllib import request
import json
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'
headers = {
     # 'Accept': '*/*',
     # 'Accept-Encoding': 'gzip, deflate, br',
     # 'Accept-Language': 'zh-CN,zh;q=0.9',
     # 'Connection': 'keep-alive',
     'Cookie': 'aliyungf_tc=AQAAAF3SkEjg5wsAckLD3GfLaoaPqH7g; xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4; xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA; xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a; xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs; _ga=GA1.2.1192984622.1534334895; _gid=GA1.2.326857848.1534334895; Hm_lvt_1db88642e346389874251b5a1eded6e3=1534334895; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534334895; u=221534334938623; device_id=5190c225596ee4927513d5631ef3b3f5',
     # 'Host': 'xueqiu.com',
     # 'Referer': 'https://xueqiu.com/',
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
     # 'X-Requested-With': 'XMLHttpRequest'
}

response = requests.get(url,headers=headers)
res =json.loads(response.content.decode('utf-8'))
res_dict = res['list']
data={}
for list_item in res_dict:
    dict_list=json.loads(list_item['data'])
    data['ids']=dict_list['id']
    data['title']=dict_list['title']
    data['description']=dict_list['description']
    data['target']=dict_list['target']
    try:
        sql = 'insert into xueqiu(ids,title,description,target) values("{ids}","{title}","{description}","{target}")'.format(**data)
        #mc = mysql_connt()
        #mc.execute_modify_mysql(sql)
    except:
        pass





